home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / pascal / windex.com / WINDEX.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1989-03-05  |  7.8 KB  |  244 lines

  1. UNIT Windex; {Windex - window tools
  2.               BlueRibbon Software    John Roncalio
  3.               34439 Ascott Avenue    603 Cherry Street, Box 432
  4.               Abbotsford, B.C.       Sumas, Washington
  5.               CANADA V2S 4V6         U.S.A. 98295 }
  6.  
  7. {$DEFINE SAFTEY} {Remove this line to make smaller/faster unit}
  8.  
  9. INTERFACE
  10. USES
  11.  CRT,
  12.  DOS;
  13.  
  14. CONST
  15.  Single : BYTE=1;   {Single and Double are the usual constants for drawing the}
  16.  Double : BYTE=2;   {box around the window.}
  17.  RAM    : BYTE = 0;
  18.  Disk   : BYTE = 1;
  19.  
  20. PROCEDURE OpenWindow(TX,TY,BX,BY,FRG,BKG,Border : BYTE;
  21.                      Heading                    : STRING;
  22.                      WhereSaved                 : BYTE);
  23. PROCEDURE CloseWindow;
  24.  
  25. IMPLEMENTATION
  26.  
  27. TYPE
  28.  WindType=RECORD
  29.   Active       : BOOLEAN; {has this window been activated?}
  30.   Location     : POINTER; {location in ram where window image was stored}
  31.   X1,Y1,X2,Y2,            {border coordinates relative to full screen}
  32.   WAttribute   : BYTE;    {TextAttr last active in this window}
  33.   WhereCursor  : WORD;    {Cursor location: Lo=X, Hi=Y relative to window}
  34.  END;
  35.  
  36. VAR
  37.  WA          : ARRAY[0..9] OF WindType; {list of window info}
  38.  Vimage      : FILE;       {window contents on disk}
  39.  LinePointer,              {dynamic pointer to current video line}
  40.  RamPlace,                 {dynamic pointer to saved line of video memory}
  41.  VideoMem    : POINTER;    {points to start of video ram}
  42.  Vname       : STRING[8];  {Vimage file name}
  43.  EndY,                     {marks last line to read/write}
  44.  RamSeg,                   {VideoMem segment}
  45.  RamOfs,                   {   "     offset}
  46.  VidOfs,                   {used to incriment VideoMem pointer}
  47.  Wsize,                    {size of window contents saved on heap/disk}
  48.  WlineSize,                {size of one line of the window}
  49.  Vseg,Voff,                {video seg,off}
  50.  Wcount      : WORD;       {multi duty counter/indexer}
  51.  WindRec     : WindType;   {info on a window}
  52.  
  53. PROCEDURE NoWindow; {LOCAL TO IMPLEMENTATION}
  54. BEGIN
  55.  WITH WindRec DO
  56.  BEGIN
  57.   Active:=False;
  58.   {$IFDEF SAFTEY}
  59.     Location:=Nil;
  60.     X1:=1; Y1:=1; X2:=80; Y2:=25;
  61.     Wattribute:=LightGray;
  62.     WhereCursor:=$101; {home: 1,1}
  63.   {$ENDIF}
  64.  END;
  65. END;
  66.  
  67. {LOCAL TO IMPLEMENTATION}
  68. PROCEDURE Box(X1,Y1,X2,Y2,LineStyle:BYTE);
  69. VAR
  70.  Tl,Tr,                 {The border line style is usually Single or Double.}
  71.  Bl,Br,Ver: CHAR;       {The ASCII charactor codes 32..254 can be used to  }
  72.  Counter,               {obtain unusual borders -- only a few of which look}
  73.  Columns,               {presentable.}
  74.  Rows            : INTEGER;
  75.  Line            : STRING[80];
  76. BEGIN
  77.  Columns:=X2-X1-1; Rows:=Y2-Y1-1;
  78.  CASE LineStyle OF
  79.   1 :BEGIN
  80.       FILLCHAR(Line,80,#196); Line[0]:=CHR(80);
  81.       Tl:=#218; Tr:=#191; Bl:=#192; Br:=#217; Ver:=#179;
  82.      END;
  83.   2 :BEGIN
  84.       FILLCHAR(Line,80,#205); Line[0]:=CHR(80);
  85.       Tl:=#201; Tr:=#187; Bl:=#200; Br:=#188; Ver:=#186;
  86.      END;
  87.   32..254:BEGIN
  88.            FillChar(Line,80,LineStyle); Line[0]:=CHR(80);
  89.            Tl:=CHR(LineStyle); Tr:=CHAR(LineStyle);
  90.            Bl:=CHR(LineStyle); Br:=CHR(LineStyle);
  91.            Ver:=CHR(LineStyle);
  92.           END;
  93.   ELSE Exit; {You don't want a border of ^G, do you?}
  94.  END; {case}
  95.  GoToXY(X1,Y1); WRITE(Tl+COPY(Line,1,Columns)+Tr);
  96.  FOR Counter:=1 TO Rows DO
  97.  BEGIN
  98.   GoToXY(X1,Y1+Counter); WRITE(Ver);
  99.   GoToXY(X2,Y1+Counter); WRITE(Ver);
  100.  END;
  101.  GoToXY(X1,Y2); WRITE(Bl+COPY(Line,1,Columns)+Br);
  102. END;
  103.  
  104. PROCEDURE OpenWindow(TX,TY,BX,BY,FRG,BKG,Border : BYTE;
  105.                      Heading                    : STRING;
  106.                      WhereSaved                 : BYTE);
  107. BEGIN
  108.  {$IFDEF SAFETY}
  109.    {exit if illegal parameters}
  110.    IF (BX<=TX+3) OR (BY<=TY+3) OR (FRG>15) OR (BKG>7) OR (FRG=BKG)
  111.       OR (Length(Heading)>BX-TX-1) THEN HALT(5);
  112.    IF (Border=0) OR (Border>2) OR (WhereSaved>1) THEN HALT(5);
  113.  {$ENDIF}
  114.  {get next available window}
  115.  Wcount:=0;
  116.  WHILE (Wcount<10) AND WA[Wcount].Active DO INC(Wcount);
  117.  {$IFDEF SAFETY}
  118.    {halt if no more slots}
  119.    IF Wcount=10 THEN
  120.    BEGIN
  121.     WindRec:=WA[0]; Window(1,1,WindRec.X2,WindRec.Y2); {restore orig window}
  122.     ClrScr; RunError(201);
  123.    END;
  124.  {$ENDIF}
  125.  {save old window info}
  126.  WA[Wcount-1].WhereCursor:=WhereY * $100 + WhereX;
  127.  WA[Wcount-1].WAttribute:=TextAttr;
  128.  Window(1,1,80,25);
  129.  {write window record}
  130.  WlineSize:=2*(BX-TX+1); Wsize:=WlineSize*(BY-TY+1);
  131.  WindRec.Active:=True;
  132.  IF WhereSaved=Disk THEN
  133.  BEGIN
  134.   WindRec.Location:=Nil;
  135.   Vname:=''; STR(Wcount:1,Vname); Vname:='WINDOW.'+Vname;
  136.   ASSIGN(Vimage,Vname); REWRITE(Vimage,WlineSize);
  137.  END
  138.  ELSE
  139.  BEGIN
  140.   GetMem(WindRec.Location,Wsize); {get memory for image & set pointer}
  141.   RamSeg:=Seg(WindRec.Location^); {stored image segment}
  142.   RamOfs:=Ofs(WindRec.Location^); {stored image offset}
  143.   RamPlace:=Ptr(RamSeg,RamOfs);
  144.  END;
  145.  WindRec.X1:=TX; WindRec.Y1:=TY; WindRec.X2:=BX; WindRec.Y2:=BY;
  146.  WA[Wcount]:=WindRec; {add new window info to list}
  147.  {save image}
  148.  EndY:=BY-TY+1;
  149.  VidOfs:=(TY-1)*160 + (TX-1)*2;
  150.  FOR Wcount:=1 TO EndY DO
  151.  BEGIN
  152.   LinePointer:=Ptr(Vseg,VidOfs);
  153.   IF WhereSaved=Disk THEN BlockWrite(Vimage,LinePointer^,1) ELSE
  154.   BEGIN
  155.    Move(LinePointer^,RamPlace^,WlineSize);
  156.    INC(RamOfs,WlineSize);
  157.    RamPlace:=Ptr(RamSeg,RamOfs);
  158.   END;
  159.   INC(VidOfs,160);
  160.  END;
  161.  TextAttr:=Bkg*16+Frg;    {set new video attributes}
  162.  Box(TX,TY,BX,BY,Border); {draw box}
  163.  {write heading}
  164.  GoToXY(TX+((BX-TX-Length(Heading)+1) DIV 2),TY);
  165.  WRITE(Heading);
  166.  Window(TX+1,TY+1,BX-1,BY-1);  {Make new window}
  167.  ClrScr;  {clear new window}
  168.  IF WhereSaved=Disk THEN CLOSE(Vimage);
  169. END;
  170.  
  171. PROCEDURE CloseWindow;
  172. CONST OnDisk:BOOLEAN=False;
  173. VAR
  174.  Ycount   : INTEGER;
  175. BEGIN
  176.  {count down to last active window}
  177.  Wcount:=9;
  178.  WHILE NOT WA[Wcount].Active DO DEC(Wcount);
  179.  IF Wcount=0 THEN EXIT; {just joking - no window was open!}
  180.  Window(1,1,80,25);
  181.  WindRec:=WA[Wcount]; 
  182.  OnDisk:=False; {Added 89/03/05}
  183.  IF WindRec.Location=Nil THEN OnDisk:=True; {image was stored on disk}
  184.  WlineSize:=2*(WindRec.X2-WindRec.X1+1);
  185.  Wsize:=WlineSize*(WindRec.Y2-WindRec.Y1+1); {size of memory to free}
  186.  Vseg:=Seg(VideoMem^);
  187.  VidOfs:=(WindRec.Y1-1)*160 + (WindRec.X1-1)*2;
  188.  LinePointer:=Ptr(Vseg,VidOfs);
  189.  IF OnDisk THEN  {restore image from disk}
  190.  BEGIN
  191.   Vname:=''; STR(Wcount:1,Vname); Vname:='WINDOW.'+Vname;
  192.   ASSIGN(Vimage,Vname); RESET(Vimage,WlineSize);
  193.   WHILE NOT EOF(Vimage) DO
  194.   BEGIN
  195.    BlockRead(Vimage,LinePointer^,1); {This reads from disk directly to video}
  196.    INC(VidOfs,160);                  {memory. On a floppy disk it sucks. For}
  197.    LinePointer:=Ptr(Vseg,VidOfs);    {snapier results can add a buffer, read}
  198.   END;                               {to the buffer, and then move the image}
  199.   CLOSE(Vimage); ERASE(Vimage);      {to video ram as shown below.}
  200.  END
  201.  ELSE  {image was stored on heap}
  202.  BEGIN
  203.   RamSeg:=Seg(WindRec.Location^);
  204.   RamOfs:=Ofs(WindRec.Location^);
  205.   RamPlace:=Ptr(RamSeg,RamOfs);
  206.   EndY:=WindRec.Y2-WindRec.Y1+1;
  207.   FOR Ycount:=1 TO EndY DO
  208.   BEGIN
  209.    Move(RamPlace^,LinePointer^,WlineSize);
  210.    INC(VidOfs,160); INC(RamOfs,WlineSize);
  211.    LinePointer:=Ptr(Vseg,VidOfs);
  212.    RamPlace:=Ptr(RamSeg,RamOfs);
  213.   END;
  214.   FreeMem(WindRec.Location,Wsize);
  215.  END;
  216.  NoWindow; WA[Wcount]:=WindRec; {mark window as closed}
  217.  {set window to lower level}
  218.  Dec(Wcount); WindRec:=WA[Wcount];
  219.  WITH WindRec DO
  220.  BEGIN
  221.   IF Wcount=0 THEN Window(X1,Y1,X2,Y2) ELSE Window(X1+1,Y1+1,X2-1,Y2-1);
  222.   TextAttr:=Wattribute;
  223.   GoToXY(Lo(WhereCursor),Hi(WhereCursor));
  224.  END;
  225. END;
  226.  
  227. BEGIN
  228.  {initialize WindowArray list}
  229.  WITH WindRec DO
  230.  BEGIN
  231.   Active:=True;
  232.   X1:=Lo(WindMin)+1;
  233.   Y1:=Hi(WindMin)+1;
  234.   X2:=Lo(WindMax)+1;
  235.   Y2:=Hi(WindMax)+1;
  236.  END;
  237.  WA[0]:=WindRec;
  238.  NoWindow;
  239.  FOR Wcount:=1 TO 9 DO WA[Wcount]:=WindRec;
  240.  {init globals}
  241.  IF Byte(Ptr($40,$49)^)=7 THEN Vseg:=$B000 ELSE Vseg:=$B800;
  242.  Voff:=0;
  243.  VideoMem:=Ptr(Vseg,Voff);
  244. END.